package com.mall.service.impl;

import com.github.pagehelper.PageInfo;
import com.mall.bean.Collection;
import com.mall.bean.ShopCart;
import com.mall.bean.ShopCartExample;
import com.mall.dao.ShopCartMapper;
import com.mall.service.ShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author lwd1513
 * @date 2022-04-25 14 11
 * discription
 */
@Service
public class ShopCartServiceImpl implements ShopCartService {
    @Autowired
    private ShopCartMapper shopCartMapper;

    @Override
    public int insert(ShopCart shopCart) {
        return 0;
    }

    @Override
    public List<ShopCart> queryData(ShopCart shopCart) {
        return null;
    }

    @Override
    public int update(ShopCart shopCart) {
        return 0;
    }

    @Override
    public int deleteById(String id) {
        return 0;
    }

    @Override
    public ShopCart queryDataById(String id) {
        return null;
    }

    @Override
    public PageInfo<ShopCart> queryDataByPage(int currentPage, int pageSize, ShopCart shopCart) {
        return null;
    }

    @Override
    public List<ShopCart> getShopCartByMemberId(String memberId) {
        ShopCartExample example = new ShopCartExample();
        ShopCartExample.Criteria criteria = example.createCriteria();
        criteria.andFlagEqualTo(1);
        criteria.andMember_idEqualTo(Integer.parseInt(memberId));
        List<ShopCart> shopCarts = shopCartMapper.selectByExample(example);
        return shopCarts;
    }

    @Override
    public int addCartGoodsById(String goodsId, String memberId) {
        ShopCart shopCart = new ShopCart();
        Date now = new Date();
        shopCart.setShop_cart_id(now.getTime() + "");
        shopCart.setAdd_time(now);
        shopCart.setSelect_num(1);
        shopCart.setFlag(1);

        shopCart.setMember_id(Integer.parseInt(memberId));
        shopCart.setGoods_id(Integer.parseInt(goodsId));

        int res = shopCartMapper.insertSelective(shopCart);
        return res;
    }

    @Override
    public ShopCart getShopCartByMemberIdAndGoodsId(String goodsId, String memberId) {
        ShopCartExample shopCartExample = new ShopCartExample();
        ShopCartExample.Criteria criteria = shopCartExample.createCriteria();
        criteria.andGoods_idEqualTo(Integer.parseInt(goodsId));
        criteria.andMember_idEqualTo(Integer.parseInt(memberId));
        criteria.andFlagEqualTo(1);

        List<ShopCart> shopCarts = shopCartMapper.selectByExample(shopCartExample);
        if (shopCarts.size() >0) {
            return shopCarts.get(0);
        }
        return null;
    }

    @Override
    public int addCartGoodsSelectNumById(String goodsId, String memberId,Integer select_num) {
        ShopCart shopCart = new ShopCart();
        shopCart.setSelect_num(select_num+1);

        ShopCartExample shopCartExample = new ShopCartExample();
        ShopCartExample.Criteria criteria = shopCartExample.createCriteria();
        criteria.andGoods_idEqualTo(Integer.parseInt(goodsId));
        criteria.andMember_idEqualTo(Integer.parseInt(memberId));
        int res = shopCartMapper.updateByExampleSelective(shopCart, shopCartExample);
        return res;
    }

    @Override
    public int removeShopItemById(String itemId) {
        ShopCart shopCart = new ShopCart();
        shopCart.setFlag(0);
        shopCart.setShop_cart_id(itemId);
        int res = shopCartMapper.updateByPrimaryKeySelective(shopCart);
        return res;
    }

    @Override
    public int batchRemoveShopItemByIdArray(String[] idArray) {
        int res = shopCartMapper.batchRemoveShopItemByIdArray(idArray);
        return res;
    }


}
